<?php

/**
 * @author Nguyen Xuan Hong
 * @copyright 2010
 */

class clsMySQL 
{ 
	var $connected;
	var $log_file = 'log/dblog.txt';
	var $log_error = 1;
	var $errorstr = '';
	
	function tryconnect($host, $username, $pasword, $dbname){
		$this->connected = @mysql_connect($host, $username, $pasword);
		if (!$this->connected){
			return false;
		}
	}
	
	function connect($host, $username, $pasword, $dbname) {
		$this->connected = @mysql_connect($host, $username, $pasword);
		if ($this->connected)
		{
			if (@mysql_select_db($dbname, $this->connected)) {
				return true;
			}
			else {
				$this->show_error('Khong tim thay database -> . MySQL reported : '.mysql_error());
				return false;				
			}
		}
		else {
			$this->show_error('Khong the ket noi den MySQL server. MySQL reported: '.mysql_error());
			return false;
		}
	}
	
	function query($input){
		@mysql_query("SET NAMES 'utf8'");
		//if(($_SESSION['lastwuery'] != $input) && (strpos($input,'INSERT') !== true)){
			$q = @mysql_query($input) or $this->show_error("<b>Lỗi MySQL Query </b> : ".mysql_error().$input,$input);
			//FB::info("RunQuery : ".$input);
			//$_SESSION['lastwuery'] = $input;
			
		//}
		return $q ;
	}
	
	function fetch_array($query_id, $type=MYSQL_BOTH){
		$fa =@mysql_fetch_array($query_id,$type);
			return $fa;
	}
	
	function query_first($input) {
		$q = $this->query($input);
		$fa = array();
		$fa = $this->fetch_array($q);
		return $fa;
	}
	
	function num_rows($query_id) {
	$nr = @mysql_num_rows($query_id);
		return $nr;
	}
	
	function show_error($input,$show=false){
		$this->errorstr = $input;
		/*
		if ($this->log_error) {
			$file_name = $this->log_file;
			$fp = fopen($file_name,'a');
			flock($fp,2);
			fwrite($fp,"### ".date('H:s:i d-m-Y')." ###\n");
			fwrite($fp,$input."\n");
			fwrite($fp,"QUERY : ".$q."\n");
			flock($fp,1);
			fclose($fp);
		}
		*/
		if($show) die($this->errorstr);
		//die($input);
	}
	function puterror(){
		die($this->errorstr);
	}
}


?>